/**
 *Created by ssiagu
 * 本类是引用 hanyeah 写的，原文地址：http://hanyeah.com/blog/post/createjs%E4%B8%ADText%E4%B8%AD%E6%96%87%E4%B8%8D%E6%8D%A2%E8%A1%8C%E7%9A%84%E9%97%AE%E9%A2%98(2).html
 * 只是在修改了标点符号出现在第一行的首位问题.
 */
// namespace:
this.createjs = this.createjs||{};

(function() {
	"use strict";


// constructor:

	function MText(text, font, color) {
		this.Text_constructor(text, font, color);

	}
	var p = createjs.extend(MText, createjs.Text);

	/**
	 * Draws multiline text.
	 * @method _drawText
	 * @param {CanvasRenderingContext2D} ctx
	 * @param {Object} o
	 * @param {Array} lines
	 * @return {Object}
	 * @protected
	 **/
	p._drawText = function(ctx, o, lines) {

		var paint = !!ctx;
		if (!paint) {
			ctx = Text._workingContext;
			ctx.save();
			this._prepContext(ctx);
		}
		var lineHeight = this.lineHeight||this.getMeasuredLineHeight();

		var maxW = 0, count = 0;
		var hardLines = String(this.text).split(/(?:\r\n|\r|\n)/);
		for (var i=0, l=hardLines.length; i<l; i++) {
			var str = hardLines[i];
			var w = null;

			if (this.lineWidth != null && (w = ctx.measureText(str).width) > this.lineWidth) {
				// text wrapping:
				// hanyeah 实现中文换行
				var words0 ;
				var words ;
				if((/[\u4e00-\u9fa5]+/).test(str)){ //含有中文
					words0=str.split(/(\s)/);
					words=[];

					for(var hi=0;hi<words0.length;hi++){
						var hs="";
						for(var hj=0;hj<words0[hi].length;hj++){
							var hjs=words0[hi][hj];
							if(!punctuation && hjs.charCodeAt(0)>255){
								if(hs!=""){
									words.push(hs);
								}
								words.push(hjs);
								hs="";
							}
							else{
								hs+=hjs;
							}
						}
						if(hs!=""){
							words.push(hs);
						}
					}
				}
				else{
					words = str.split(/(\s)/);
				}
				str = words[0];
				w = ctx.measureText(str).width;

				var nextStr;
				for (var j=1, jl=words.length; j<jl; j+=1) {
					// Line needs to wrap:
					nextStr=words[j];
					var wordW = ctx.measureText(nextStr).width;
					if (w + wordW > this.lineWidth) {
						//处理标点符号，不要出现在每行的第一个位置。没有找到标点符号的正则。
						var punctuation=(nextStr && nextStr.length==1) && (/[.<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]+/).test(nextStr);
						//if(isU)console.log("str",str,punctuation,nextStr);
						if(punctuation){str +=nextStr}
						if (paint) { this._drawTextLine(ctx, str, count*lineHeight); }
						if (lines) { lines.push(str); }
						if (w > maxW) { maxW = w; }
						if(punctuation && (j+1)<jl){
							str = words[j+1];
						}else {
							str = nextStr;
						}
						w = ctx.measureText(str).width;
						count++;
					} else {
						str += nextStr;
						w += wordW;
					}
				}

			}

			//end hanyeah 实现中文换行
			if (paint) { this._drawTextLine(ctx, str, count*lineHeight); }
			if (lines) { lines.push(str); }
			if (o && w == null) { w = ctx.measureText(str).width; }
			if (w > maxW) { maxW = w; }
			count++;
		}

		if (o) {
			o.width = maxW;
			o.height = count*lineHeight;
		}
		if (!paint) { ctx.restore(); }
		return o;
	};

	createjs.MText = createjs.promote(MText, "Text");
	//createjs.MText = MText;
}());